package demo.test;

import netscape.javascript.JSObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


@RequestMapping("/test")
@RestController
public class Demo {

    @RequestMapping("/r1")
    public String r1(String name){
        return "接收到：" + name;
    }

    @RequestMapping("/r2")
    public String r2(Integer age){
        return "接收到：" + age;
    }

    @RequestMapping("r3")
    public String r3(int age){
        return "接收到：" + age;
    }

    @RequestMapping("/r4")
    public String r4(String name, Integer age){
        return "name：" + name + "，age：" + age;
    }

    @RequestMapping("/r5")
    public String r5(Student student){
        return "接收到：" + student;
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam("name") String Name, Integer age){
        return "name：" + Name + "，age：" + age;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam(value = "name",required = false) String Name){
        return "接收到：" + Name;
    }

    @RequestMapping("/r8")
    public String r8(String[] arr){
//        return "接收到："+ arr;  //hash值
        return "接收到：" + Arrays.toString(arr);
    }

    @RequestMapping("/r9")
    public String r9(@RequestParam(required = false) List<String> list){
        return "接收到：" + list;
    }

    @RequestMapping("/r10")
    public String r10(@RequestBody Student student){
        return "接收到：" + student;
    }

//    @RequestMapping("/article/{articleId}/{name}")
//    public String r11(@PathVariable("articleId") Integer id, @PathVariable("name") String Name){
//        return "接收到：" + id + "，" + Name;
//    }

    @RequestMapping("/article/{articleId}/{name}")
    public String r12(@PathVariable Integer articleId,@PathVariable String name){
//        log.info("接收参数：{}", JSObject.toJSONString(name));
//        System.out.println(name);
        return "接收到：" + articleId + "，" + name;
    }

    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile imgFile) throws IOException {
        String originalFileName = imgFile.getOriginalFilename();
        imgFile.transferTo(new File("D:" + imgFile.getOriginalFilename()));
        return "接收到文件名：" + originalFileName;
    }
}
